var ns, app, dialog, toast;
app = new Vue({
    el: '#app',
    data: {
        list: [],
        form: {
            pageindex: 0,
            pagesize: 10,
            status: 2, //1-暂存 2-处理中 3-已完成
            isFirstLoad: 1,
            ProjectIDs: [],
            keywords: '',
            TaskTypeIDs: [],
            TaskTagIDs: [],
            startTime: '',
            endTime: '',
            activeDate: 0,
            startTimeLast: '',
            endTimeLast: '',
            activeDateLast: 0,
            TaskSearchStatus:0,
        },
        total: {
            WaitSendCount:0,
            HandleCount: 0,//进行中数量
            CompleteCount:0,
        },
        current_item_length: 0,
        is_searching: false,
        scroll_top: 0,
        can_scroll: false,
        showsearchmonth: false,
        show: false,
        showsearch: false,
        showTop: false,
        projectList: [],
        TaskTypeList:[],
        TaskTagList:[],
        TaskSearchList:[
            {ID:0,Name:'全部'},
            {ID:2,Name:'我负责的'},
            {ID:3,Name:'我参与的'},
            {ID:1,Name:'我创建的'},
        ],
    },
    methods: {
        doReset: function (type) {
            var that = this;
            if (type == 2) {
            } else {
                that.form.TaskTypeIDs = [];
                that.form.TaskTagIDs = [];
                that.form.startTime= '';
                that.form.endTime= '';
                that.form.activeDate= 0;
                that.form.startTimeLast= '';
                that.form.endTimeLast= '';
                that.form.activeDateLast= 0;
            }
        },
        do_search: function () {
            var that = this;
            that.form.isFirstLoad = 1;
            that.doHideSearch();
            that.get_data();
        },
        doSelectSearchStatus: function (ID) {
            var that = this;
            that.form.TaskSearchStatus = ID;
            that.do_search();
        },
        doSelectType: function (ID) {
            var that = this;
            var index = that.form.TaskTypeIDs.indexOf(ID);
            if (index > -1) {
                that.form.TaskTypeIDs.splice(index, 1);
            } else {
                that.form.TaskTypeIDs.push(ID);
            }
        },
        doSelectTag: function (ID) {
            var that = this;
            var index = that.form.TaskTagIDs.indexOf(ID);
            if (index > -1) {
                that.form.TaskTagIDs.splice(index, 1);
            } else {
                that.form.TaskTagIDs.push(ID);
            }
        },
        getworktaskother: function () {
            var that = this;
            var options = {};
            options.action = "getworktasksearchdata";
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    that.TaskTypeList = data.data.TypeList;
                    that.TaskTagList = data.data.TagList;
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },  
        do_open_search: function () {
            var that = this;
            that.show = !that.show;
            setTimeout(function () {
                that.initPicker();
            }, 100);
        },
        get_data: function () {
            var that = this;
            that.is_searching = true;
            that.can_scroll = false;
            var options = {};
            options.action = "getworktaskdatagrid";
            options.KeyWords = that.form.keywords;
            options.StartTime1 = that.form.startTime;
            options.StartTime2 = that.form.endTime;
            options.EndTime1 = that.form.startTimeLast;
            options.EndTime2 = that.form.endTimeLast;
            options.TypeIDs = JSON.stringify(that.form.TaskTypeIDs);
            options.TagIDs =  JSON.stringify(that.form.TaskTagIDs);
            var HandleUserIDs=[];
            options.HandleUserIDs =JSON.stringify(HandleUserIDs);
            options.SearChType = that.form.TaskSearchStatus;
            options.Status = that.form.status;
            options.pagesize = that.form.pagesize;
            options.pageindex = that.form.pageindex;
            // console.log(JSON.stringify(options));
            ns.post(options, function (succeed, data, err) {
                // console.log(JSON.stringify(data));
                that.is_searching = false;
                if (succeed) {
                    if (data.list.length == that.form.pagesize) {
                        that.can_scroll = true;
                    }
                    if (that.form.pageindex == 0) {
                        that.list = data.list;
                    } else {
                        that.list = that.list.concat(data.list);
                    }
                    if (that.form.isFirstLoad == 1) {
                        that.total.WaitSendCount = data.SummaryData.WaitSendCount||0;//暂存
                        that.total.HandleCount = data.SummaryData.HandleCount||0; //进行中
                        that.total.CompleteCount= data.SummaryData.CompleteCount||0; //已完成
                    }
                    that.current_item_length = that.list.length;
                    that.form.isFirstLoad = 0;
                } else if (err) {
                    that.current_item_length = 0;
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });
        },
        select_status: function (status) {
            var that = this;
            that.show = false;
            that.showTop = false;
            that.form.status = status;
            that.list = [];
            that.form.isFirstLoad = 1;
            that.form.TaskSearchStatus=0;
            that.get_data();
        },
        doClickDay: function (dataValue) {
            var that = this;
            that.form.activeDate = dataValue;
            var startDate = '',
                endDate = '';
            if (dataValue == 1) {
                var currentDate = dateRangeUtil.getCurrentDate();
                startDate= moment(new Date()).subtract(3, 'days').format("YYYY-MM-DD");
                endDate =  dateRangeUtil.getDateString(currentDate);
                that.form.activeDateDesc = '近三天';
            }
            if (dataValue == 2) {
                var currentDate = dateRangeUtil.getCurrentDate();
                startDate= moment(new Date()).subtract(7, 'days').format("YYYY-MM-DD");
                endDate= dateRangeUtil.getDateString(currentDate);
                that.form.activeDateDesc = '近七天';
            }
            if (dataValue == 3) {
                var currentDate = dateRangeUtil.getCurrentDate();
                startDate= moment(new Date()).subtract(30, 'days').format("YYYY-MM-DD");
                endDate= dateRangeUtil.getDateString(currentDate);
                that.form.activeDateDesc = '近三十天';
            }
            that.form.startTime = startDate;
            that.form.endTime = endDate;
        },
        doClickDayLast: function (dataValue) {
            var that = this;
            that.form.activeDateLast = dataValue;
            var startDate = '',
                endDate = '';
            if (dataValue == 1) {
                var currentDate = dateRangeUtil.getCurrentDate();
                startDate= moment(new Date()).subtract(3, 'days').format("YYYY-MM-DD");
                endDate =  dateRangeUtil.getDateString(currentDate);
            }
            if (dataValue == 2) {
                var currentDate = dateRangeUtil.getCurrentDate();
                startDate= moment(new Date()).subtract(7, 'days').format("YYYY-MM-DD");
                endDate= dateRangeUtil.getDateString(currentDate);
            }
            if (dataValue == 3) {
                var currentDate = dateRangeUtil.getCurrentDate();
                startDate= moment(new Date()).subtract(30, 'days').format("YYYY-MM-DD");
                endDate= dateRangeUtil.getDateString(currentDate);
            }
            that.form.startTimeLast = startDate;
            that.form.endTimeLast = endDate;
        },
        addWorkTask(){
            var that = this;
            ns.openWin('worktaskadd', '新增工作任务', {});
        },
        open_list: function (item) {
            var that = this;
            ns.openWin('worktaskdetail_frm', '任务详情', {
                id: item.ID,
            });
        },
      
        doHideSearch: function () {
            var that = this;
            that.showTop = false;
            that.showsearch = false;
            that.show = false;
        },
        initPicker: function () {
            var _this = this;
            (function ($, doc) {
                $.init();
                $.ready(function () {
                    //开始日期
                    var btn = document.querySelector('#startdate');
                    btn.addEventListener('tap', function () {
                        var _self = this;
                        if (_self.picker) {
                            if(_this.form.startTime){
                                _self.picker.setSelectedValue(_this.form.startTime);//回显
                            }
                            _self.picker.show(function (rs) {
                                _this.form.startTime = rs.value;
                                _self.picker.dispose();
                                _self.picker = null;
                            });
                        } else {
                            var optionsJson = this.getAttribute('data-options') || '{}';
                            var options = JSON.parse(optionsJson);
                            var id = this.getAttribute('id');
                            _self.picker = new $.DtPicker(options);
                            if(_this.form.startTime){
                                _self.picker.setSelectedValue(_this.form.startTime);//回显
                            }
                            _self.picker.show(function (rs) {
                                var timer = setTimeout(function () {
                                    clearTimeout(timer);
                                    _this.form.startTime = rs.value;
                                }, 300);
                                _self.picker.dispose();
                                _self.picker = null;
                            });
                        }
                    }, false);
                    //结束日期
                    var btnEnd = document.querySelector('#enddate');
                    btnEnd.addEventListener('tap', function () {
                        var _self = this;
                        if (_self.picker) {
                            if(_this.form.endTime){
                                _self.picker.setSelectedValue(_this.form.endTime);//回显
                            }
                            _self.picker.show(function (rs) {
                                _this.form.endTime = rs.value;
                                _self.picker.dispose();
                                _self.picker = null;
                            });
                        } else {
                            var optionsJson = this.getAttribute('data-options') || '{}';
                            var options = JSON.parse(optionsJson);
                            var id = this.getAttribute('id');
                            _self.picker = new $.DtPicker(options);
                            if(_this.form.endTime){
                                _self.picker.setSelectedValue(_this.form.endTime);//回显
                            }
                            _self.picker.show(function (rs) {
                                var timer = setTimeout(function () {
                                    clearTimeout(timer);
                                    _this.form.endTime = rs.value;
                                }, 300);
                                _self.picker.dispose();
                                _self.picker = null;
                            });
                        }
                    }, false);
                     //开始日期-Last
                     var btnLast = document.querySelector('#startdateLast');
                     btnLast.addEventListener('tap', function () {
                         var _self = this;
                         if (_self.picker) {
                             if(_this.form.startTimeLast){
                                 _self.picker.setSelectedValue(_this.form.startTimeLast);//回显
                             }
                             _self.picker.show(function (rs) {
                                 _this.form.startTimeLast = rs.value;
                                 _self.picker.dispose();
                                 _self.picker = null;
                             });
                         } else {
                             var optionsJson = this.getAttribute('data-options') || '{}';
                             var options = JSON.parse(optionsJson);
                             var id = this.getAttribute('id');
                             _self.picker = new $.DtPicker(options);
                             if(_this.form.startTimeLast){
                                 _self.picker.setSelectedValue(_this.form.startTimeLast);//回显
                             }
                             _self.picker.show(function (rs) {
                                 var timer = setTimeout(function () {
                                     clearTimeout(timer);
                                     _this.form.startTimeLast = rs.value;
                                 }, 300);
                                 _self.picker.dispose();
                                 _self.picker = null;
                             });
                         }
                     }, false);
                     //结束日期
                     var btnEndLast = document.querySelector('#enddateLast');
                     btnEndLast.addEventListener('tap', function () {
                         var _self = this;
                         if (_self.picker) {
                             if(_this.form.endTimeLast){
                                 _self.picker.setSelectedValue(_this.form.endTimeLast);//回显
                             }
                             _self.picker.show(function (rs) {
                                 _this.form.endTimeLast = rs.value;
                                 _self.picker.dispose();
                                 _self.picker = null;
                             });
                         } else {
                             var optionsJson = this.getAttribute('data-options') || '{}';
                             var options = JSON.parse(optionsJson);
                             var id = this.getAttribute('id');
                             _self.picker = new $.DtPicker(options);
                             if(_this.form.endTimeLast){
                                 _self.picker.setSelectedValue(_this.form.endTimeLast);//回显
                             }
                             _self.picker.show(function (rs) {
                                 var timer = setTimeout(function () {
                                     clearTimeout(timer);
                                     _this.form.endTimeLast = rs.value;
                                 }, 300);
                                 _self.picker.dispose();
                                 _self.picker = null;
                             });
                         }
                     }, false);
                });
            })(mui, document);
        },
    }
});
apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;
    dialog = new auiDialog();
    toast = new auiToast();
    app.form.isviewdetail = api.pageParam.isviewdetail || false;
    if (app.form.isviewdetail) {
        app.form.status = 4;
    }
    api.addEventListener({
        name: 'reloadworktasklist'
    }, function (ret, err) {
        app.form.isFirstLoad = 1;
        app.get_data();
    });

    ns.init_pull();
    ns.init_search();
    app.getworktaskother();
    app.get_data();
};
